import { Router, IRouter } from 'express';
import { authenticate } from '../middleware/auth';
import { AIController, createDesignValidation, designIdValidation, updateDesignPublicStatusValidation, createTextToImageValidation } from '../controllers/AIController';
import { uploadImage, handleUploadError } from '../middleware/upload';

const router: IRouter = Router();
const aiController = new AIController();

/**
 * @swagger
 * components:
 *   schemas:
 *     Design:
 *       type: object
 *       properties:
 *         id:
 *           type: string
 *           format: uuid
 *           description: 设计ID
 *         userId:
 *           type: integer
 *           description: 用户ID
 *         roomType:
 *           type: string
 *           enum: [living, bedroom, kitchen, bathroom, office, dining]
 *           description: 房间类型
 *         designStyle:
 *           type: string
 *           enum: [modern, traditional, scandinavian, industrial, bohemian, minimalist]
 *           description: 设计风格
 *         originalImageUrl:
 *           type: string
 *           format: uri
 *           description: 原始图片URL
 *         generatedImageUrl:
 *           type: string
 *           format: uri
 *           description: 生成的图片URL
 *         prompt:
 *           type: string
 *           description: 生成提示词
 *         status:
 *           type: string
 *           enum: [pending, processing, completed, failed]
 *           description: 设计状态
 *         errorMessage:
 *           type: string
 *           description: 错误信息
 *         creditsUsed:
 *           type: integer
 *           description: 使用的积分数
 *         openaiRequestId:
 *           type: string
 *           description: OpenAI请求ID
 *         generationTimeMs:
 *           type: integer
 *           description: 生成耗时（毫秒）
 *         createdAt:
 *           type: string
 *           format: date-time
 *           description: 创建时间
 *         updatedAt:
 *           type: string
 *           format: date-time
 *           description: 更新时间
 *     CreateDesignRequest:
 *       type: object
 *       required:
 *         - roomType
 *         - designStyle
 *       properties:
 *         roomType:
 *           type: string
 *           enum: [living, bedroom, kitchen, bathroom, office, dining]
 *           description: 房间类型
 *         designStyle:
 *           type: string
 *           enum: [modern, traditional, scandinavian, industrial, bohemian, minimalist]
 *           description: 设计风格
 *         customPrompt:
 *           type: string
 *           maxLength: 1000
 *           description: 自定义提示词（可选）
 *     DesignResult:
 *       type: object
 *       properties:
 *         id:
 *           type: string
 *           format: uuid
 *           description: 设计ID
 *         status:
 *           type: string
 *           enum: [pending, processing, completed, failed]
 *           description: 设计状态
 *         generatedImageUrl:
 *           type: string
 *           format: uri
 *           description: 生成的图片URL
 *         creditsUsed:
 *           type: integer
 *           description: 使用的积分数
 *         errorMessage:
 *           type: string
 *           description: 错误信息
 *     DesignListResult:
 *       type: object
 *       properties:
 *         designs:
 *           type: array
 *           items:
 *             $ref: '#/components/schemas/Design'
 *         total:
 *           type: integer
 *           description: 总数量
 *         page:
 *           type: integer
 *           description: 当前页码
 *         limit:
 *           type: integer
 *           description: 每页数量
 *         totalPages:
 *           type: integer
 *           description: 总页数
 *     RoomType:
 *       type: object
 *       properties:
 *         value:
 *           type: string
 *           description: 房间类型值
 *         label:
 *           type: string
 *           description: 房间类型标签
 *         description:
 *           type: string
 *           description: 房间类型描述
 *     DesignStyle:
 *       type: object
 *       properties:
 *         value:
 *           type: string
 *           description: 设计风格值
 *         label:
 *           type: string
 *           description: 设计风格标签
 *         description:
 *           type: string
 *           description: 设计风格描述
 *     DesignStats:
 *       type: object
 *       properties:
 *         total:
 *           type: integer
 *           description: 总设计数
 *         completed:
 *           type: integer
 *           description: 已完成数
 *         processing:
 *           type: integer
 *           description: 处理中数
 *         failed:
 *           type: integer
 *           description: 失败数
 *         pending:
 *           type: integer
 *           description: 待处理数
 */

/**
 * @swagger
 * /api/v1/ai/designs:
 *   post:
 *     summary: 创建AI设计任务
 *     tags: [AI Design]
 *     security:
 *       - bearerAuth: []
 *     requestBody:
 *       required: true
 *       content:
 *         multipart/form-data:
 *           schema:
 *             type: object
 *             required:
 *               - roomType
 *               - designStyle
 *               - image
 *             properties:
 *               roomType:
 *                 type: string
 *                 enum: [living, bedroom, kitchen, bathroom, office, dining]
 *                 description: 房间类型
 *               designStyle:
 *                 type: string
 *                 enum: [modern, traditional, scandinavian, industrial, bohemian, minimalist]
 *                 description: 设计风格
 *               customPrompt:
 *                 type: string
 *                 maxLength: 1000
 *                 description: 自定义提示词（可选）
 *               image:
 *                 type: string
 *                 format: binary
 *                 description: 房间图片
 *     responses:
 *       201:
 *         description: AI设计任务创建成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                 data:
 *                   $ref: '#/components/schemas/DesignResult'
 *                 message:
 *                   type: string
 *       400:
 *         description: 请求参数错误或积分不足
 *       401:
 *         description: 用户未认证
 *       503:
 *         description: AI服务不可用
 */
router.post('/designs', authenticate, uploadImage('image'), createDesignValidation, aiController.createDesign, handleUploadError);

/**
 * @swagger
 * /api/v1/ai/designs/text-to-image:
 *   post:
 *     summary: 创建文生图设计任务
 *     tags: [AI Design]
 *     security:
 *       - bearerAuth: []
 *     requestBody:
 *       required: true
 *       content:
 *         multipart/form-data:
 *           schema:
 *             type: object
 *             required:
 *               - customPrompt
 *               - image
 *             properties:
 *               customPrompt:
 *                 type: string
 *                 maxLength: 1000
 *                 description: 用户输入的设计提示词
 *               image:
 *                 type: string
 *                 format: binary
 *                 description: 房间图片
 *     responses:
 *       201:
 *         description: 文生图设计任务创建成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                 data:
 *                   $ref: '#/components/schemas/DesignResult'
 *                 message:
 *                   type: string
 *       400:
 *         description: 请求参数错误或积分不足
 *       401:
 *         description: 用户未认证
 *       503:
 *         description: AI服务不可用
 */
router.post('/designs/text-to-image', authenticate, uploadImage('image'), createTextToImageValidation, aiController.createTextToImageDesign, handleUploadError);

/**
 * @swagger
 * /api/v1/ai/designs/public:
 *   get:
 *     summary: 获取公开设计列表（无需认证）
 *     tags: [AI Design]
 *     parameters:
 *       - in: query
 *         name: page
 *         schema:
 *           type: integer
 *           minimum: 1
 *           default: 1
 *         description: 页码
 *       - in: query
 *         name: limit
 *         schema:
 *           type: integer
 *           minimum: 1
 *           maximum: 100
 *           default: 10
 *         description: 每页数量
 *       - in: query
 *         name: roomType
 *         schema:
 *           type: string
 *           enum: [living, bedroom, kitchen, bathroom, office, dining]
 *         description: 房间类型筛选
 *       - in: query
 *         name: designStyle
 *         schema:
 *           type: string
 *           enum: [modern, traditional, scandinavian, industrial, bohemian, minimalist]
 *         description: 设计风格筛选
 *     responses:
 *       200:
 *         description: 获取公开设计列表成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                 data:
 *                   $ref: '#/components/schemas/DesignListResult'
 *                 message:
 *                   type: string
 */
router.get('/designs/public', aiController.getPublicDesigns);

/**
 * @swagger
 * /api/v1/ai/designs:
 *   get:
 *     summary: 获取用户设计列表
 *     tags: [AI Design]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: query
 *         name: page
 *         schema:
 *           type: integer
 *           minimum: 1
 *           default: 1
 *         description: 页码
 *       - in: query
 *         name: limit
 *         schema:
 *           type: integer
 *           minimum: 1
 *           maximum: 100
 *           default: 10
 *         description: 每页数量
 *       - in: query
 *         name: roomType
 *         schema:
 *           type: string
 *           enum: [living, bedroom, kitchen, bathroom, office, dining]
 *         description: 房间类型筛选
 *       - in: query
 *         name: designStyle
 *         schema:
 *           type: string
 *           enum: [modern, traditional, scandinavian, industrial, bohemian, minimalist]
 *         description: 设计风格筛选
 *     responses:
 *       200:
 *         description: 获取设计列表成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                 data:
 *                   $ref: '#/components/schemas/DesignListResult'
 *                 message:
 *                   type: string
 *       401:
 *         description: 用户未认证
 */
router.get('/designs', authenticate, aiController.getUserDesigns);

/**
 * @swagger
 * /api/v1/ai/designs/{designId}:
 *   get:
 *     summary: 获取设计详情
 *     tags: [AI Design]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: designId
 *         required: true
 *         schema:
 *           type: string
 *           format: uuid
 *         description: 设计ID
 *     responses:
 *       200:
 *         description: 获取设计详情成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                 data:
 *                   $ref: '#/components/schemas/Design'
 *                 message:
 *                   type: string
 *       401:
 *         description: 用户未认证
 *       404:
 *         description: 设计记录不存在
 */
/**
 * @swagger
 * /api/v1/ai/designs/stats:
 *   get:
 *     summary: 获取设计统计信息
 *     tags: [AI Design]
 *     security:
 *       - bearerAuth: []
 *     responses:
 *       200:
 *         description: 获取设计统计成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                 data:
 *                   $ref: '#/components/schemas/DesignStats'
 *                 message:
 *                   type: string
 *       401:
 *         description: 用户未认证
 */
router.get('/designs/stats', authenticate, aiController.getDesignStats);

router.get('/designs/:designId', authenticate, designIdValidation, aiController.getDesign);

/**
 * @swagger
 * /api/v1/ai/designs/{designId}/public-status:
 *   patch:
 *     summary: 更新设计公开状态
 *     tags: [AI Design]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: designId
 *         required: true
 *         schema:
 *           type: string
 *           format: uuid
 *         description: 设计ID
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - isPublic
 *             properties:
 *               isPublic:
 *                 type: boolean
 *                 description: 是否公开设计
 *     responses:
 *       200:
 *         description: 设计公开状态更新成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                 data:
 *                   $ref: '#/components/schemas/Design'
 *                 message:
 *                   type: string
 *       400:
 *         description: 请求参数错误
 *       401:
 *         description: 用户未认证
 *       404:
 *         description: 设计记录不存在
 */
router.patch('/designs/:designId/public-status', authenticate, updateDesignPublicStatusValidation, aiController.updateDesignPublicStatus);

/**
 * @swagger
 * /api/v1/ai/designs/{designId}:
 *   delete:
 *     summary: 删除设计记录
 *     tags: [AI Design]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: designId
 *         required: true
 *         schema:
 *           type: string
 *           format: uuid
 *         description: 设计ID
 *     responses:
 *       200:
 *         description: 设计记录删除成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                 message:
 *                   type: string
 *       401:
 *         description: 用户未认证
 *       404:
 *         description: 设计记录不存在
 */
router.delete('/designs/:designId', authenticate, designIdValidation, aiController.deleteDesign);

/**
 * @swagger
 * /api/v1/ai/room-types:
 *   get:
 *     summary: 获取支持的房间类型
 *     tags: [AI Design]
 *     responses:
 *       200:
 *         description: 获取房间类型成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                 data:
 *                   type: array
 *                   items:
 *                     $ref: '#/components/schemas/RoomType'
 *                 message:
 *                   type: string
 */
router.get('/room-types', aiController.getRoomTypes);

/**
 * @swagger
 * /api/v1/ai/design-styles:
 *   get:
 *     summary: 获取支持的设计风格
 *     tags: [AI Design]
 *     responses:
 *       200:
 *         description: 获取设计风格成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                 data:
 *                   type: array
 *                   items:
 *                     $ref: '#/components/schemas/DesignStyle'
 *                 message:
 *                   type: string
 */
router.get('/design-styles', aiController.getDesignStyles);

/**
 * @swagger
 * /api/v1/ai/provider/status:
 *   get:
 *     summary: 获取AI提供商状态
 *     tags: [AI Provider]
 *     security:
 *       - bearerAuth: []
 *     responses:
 *       200:
 *         description: 获取AI提供商状态成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                 data:
 *                   type: object
 *                   properties:
 *                     currentProvider:
 *                       type: string
 *                       enum: [openai, wanx]
 *                       description: 当前使用的AI提供商
 *                     availableProviders:
 *                       type: array
 *                       items:
 *                         type: string
 *                       description: 可用的AI提供商列表
 *                     providerStatus:
 *                       type: object
 *                       description: 各提供商的状态信息
 *                 message:
 *                   type: string
 *       401:
 *         $ref: '#/components/responses/Unauthorized'
 */
router.get('/provider/status', authenticate, aiController.getAIProviderStatus);

/**
 * @swagger
 * /api/v1/ai/provider/switch:
 *   post:
 *     summary: 切换AI提供商
 *     tags: [AI Provider]
 *     security:
 *       - bearerAuth: []
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - provider
 *             properties:
 *               provider:
 *                 type: string
 *                 enum: [openai, wanx]
 *                 description: 要切换到的AI提供商
 *     responses:
 *       200:
 *         description: 切换AI提供商成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                 data:
 *                   type: object
 *                   properties:
 *                     currentProvider:
 *                       type: string
 *                       description: 当前使用的AI提供商
 *                     availableProviders:
 *                       type: array
 *                       items:
 *                         type: string
 *                       description: 可用的AI提供商列表
 *                 message:
 *                   type: string
 *       400:
 *         description: 请求参数错误或提供商不可用
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/ErrorResponse'
 *       401:
 *         $ref: '#/components/responses/Unauthorized'
 */
router.post('/provider/switch', authenticate, aiController.switchAIProvider);

export default router;